package com.koo.modules.bja.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.modules.bja.dao.BjaInspectionOrderDetailDao;
import com.koo.modules.bja.dao.BjaOrderOutDao;
import com.koo.modules.bja.entity.BjaInspectionOrderDetailEntity;
import com.koo.modules.bja.entity.BjaOrderOutEntity;
import com.koo.modules.bja.service.BjaInspectionOrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BjaInspectionOrderDetailServiceImpl extends ServiceImpl<BjaInspectionOrderDetailDao, BjaInspectionOrderDetailEntity> implements BjaInspectionOrderDetailService {

    @Autowired
    private BjaOrderOutDao bjaOrderOutDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Object key = params.get("key");
        LambdaQueryWrapper<BjaInspectionOrderDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(MyStrUtil.isNotEmpty(key), BjaInspectionOrderDetailEntity::getDetailId, key)
                .or().eq(MyStrUtil.isNotEmpty(key), BjaInspectionOrderDetailEntity::getGenericName, key);

        IPage<BjaInspectionOrderDetailEntity> page = this.page(
                new Query<BjaInspectionOrderDetailEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<BjaInspectionOrderDetailEntity> listOutOrderDetails(String headQuarterAllotNo) {
        LambdaQueryWrapper<BjaOrderOutEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BjaOrderOutEntity::getHeadQuarterAllotNo, headQuarterAllotNo);
        List<BjaOrderOutEntity> bjaOrderOuts = bjaOrderOutDao.selectList(wrapper);
        //将销售出库（配货/退货）转换为入库单明细
        return bjaOrderOuts.stream().map(item -> {
            BjaInspectionOrderDetailEntity detail = new BjaInspectionOrderDetailEntity();
            BeanUtils.copyProperties(item, detail);
            detail.setInspectionType(item.getOutType());
            detail.setInspectionOrderNo(item.getHeadQuarterAllotNo());
            return detail;
        }).collect(Collectors.toList());
    }

}